extends "res://tanks/Tank.gd"


onready var parent = get_parent()

export (float) var turret_speed  # 炮台旋转速度
export (float) var detect_radius # 探测半径

var target = null # 瞄准对象

func _ready():
	$DetectRadius/CollisionShape2D.shape.radius = detect_radius

func control(delta) :
	if parent is PathFollow2D:
		parent.set_offset(parent.get_offset() +speed * delta)
		position = Vector2()
	else:
		pass
			
 

func _process(delta):
	if target:
		
		
		var target_dir = (target.global_position - global_position).normalized()
		var current_dir = Vector2(1,0).rotated($Turret.global_rotation)
		# rotated
		
		print(target_dir)
		print($Turret.global_rotation)
		print(current_dir)
		print(Vector2(0,1).rotated($Turret.global_rotation))
		print(Vector2(0,1).angle())
		print("==========")
	
		
		$Turret.global_rotation = current_dir.linear_interpolate(target_dir, turret_speed*delta).angle()


func _on_DetectRadius_body_entered(body):
	if body.name == "player" :
		target = body
	pass # Replace with function body.


func _on_DetectRadius_body_exited(body):
	if body == target:
		target = null
	pass # Replace with function body.
